package edu.vt.ece_3574.amber_alert.location;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;

import edu.vt.ece_3574.amber_alert.AmberAlertApplication;
import edu.vt.ece_3574.amber_alert.AlertSettings.Battery;
import edu.vt.ece_3574.amber_alert.AlertSettings.PollingInterval;

/**
 * This class handles checking all incoming messages and determining what to do with them
 * 
 * @author acorvin
 */
public class SendCurrentLocationReceiver extends BroadcastReceiver {
	LocationManager locationManager = LocationManagerHandler.getLocationManager();

	MapController mapController;

	GeoPoint mapPoint;

	Location lastLocation;

	public static String longitude;

	public static String latitude;

	public static String batLevel;

	public static String battery;

	private static final String TAG = SendCurrentLocationReceiver.class.getSimpleName();

	@Override
	public void onReceive(Context context, Intent intent) {
		final String SMS_EXTRA_NAME = "pdus";
		Log.d(TAG, "sms received");
		//Keep the message from being displayed to the user
		this.abortBroadcast();

		Bundle extras = intent.getExtras();

		//Get all of the incoming messages
		if ( extras != null ) {
			Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );
			//Create a message from the incoming object
			for ( int i = 0; i < smsExtra.length; ++i ) {
				SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
				//Get the body of the message
				String messageBody = sms.getMessageBody();

				//Class handles incoming text messages so I put my Battery code with it
				//Handles Battery settings through text.
				if (messageBody.contains("BATTERY:"))
					batterySetting(messageBody);
				else if(messageBody.contains("INTERVAL:"))
					intervalSetting(messageBody, context);
				else {
					//If the message wasn't one that we want to handle, display it to the user
					this.clearAbortBroadcast();
				}
			}
		}
	}
	/**
	 * Specifies minimum battery level and whether battery alert is turned on
	 * @param messageBody
	 */
	public void batterySetting(String messageBody) {
		Log.d(TAG, "sms is battery related");
		String[] messageParts = messageBody.split(":");
		battery = messageParts[1];
		batLevel = messageParts[2];
		Battery.BatStatus(battery);
		Battery.setBatLevel(batLevel);
		Log.d(TAG, "Received Battery Level: "+battery+":"+batLevel);
	}
	/**
	 * Processes polling interval which specifies how often data about member should be collected
	 * @param messageBody
	 * @param sms
	 */
	public void intervalSetting(String messageBody, Context context) {
		Log.d(TAG, "sms is interval related");	
		String[] messageParts = messageBody.split(":");
		int newInterval = Integer.parseInt(messageParts[1]) * 60000;
		PollingInterval.setPollInterval(newInterval);
		SharedPreferences intervalSettings = context.getSharedPreferences(AmberAlertApplication.defaultInterval, 0);
		SharedPreferences.Editor editor = intervalSettings.edit();
		editor.putString("interval", Integer.toString(newInterval));
		editor.commit();
		Log.d(TAG, "Received Interval: " + Integer.toString(newInterval));
	}
}
